package common;

import models.Serie;
import play.Logger;
import play.libs.WS;
import play.libs.WS.HttpResponse;
import play.libs.WS.WSRequest;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class iMDBRequest {

	
	public static JsonElement executeRequest(String imdbId) {

	
	    WSRequest request = WS.url("http://imdbapi.org/?id=" + imdbId + "&type=json&plot=full&episode=1&lang=en-US&aka=simple&release=simple");
	    //request.mimeType = "application/json";
	    
	    HttpResponse response = null;
	    
	    try {
	    	response = request.get();
	    }
	    catch (Exception exc) {
	        System.err.println("error in imdb json");
	    }


	    if (response.getStatus() != 200) {
	    	return null;
	    }
	    
	    
	    
	    return response.getJson();
	  }
	
	
	public static void fillSeries2(Serie series) {

		System.out.println(series.getName());

	    WSRequest request = WS.url("http://imdbapi.org/?title=" + series.getName() + "&type=json&plot=full&episode=0&limit=1&yg=0&mt=TVS&lang=en-US&offset=&aka=simple&release=simple");
	    request.mimeType = "application/json";
	    
	    HttpResponse response = null;
	    
	    try {
	    	response = request.get();
	    }
	    catch (Exception exc) {
	        System.err.println("error in imdb json");
	    }


	    if (response.getStatus() != 200) {
	    	return;
	    }
	    System.out.println(response.getString());
	    JsonElement je = response.getJson();

	   
	    System.out.println("getson sonrasi");
	    
	    System.out.println(je);
	    System.out.println(je.isJsonObject());
	    if ( je == null || je.isJsonObject())
	    	return;

	    System.out.println("if sonrasi");
	    JsonObject jo = je.getAsJsonObject();
	    
	    
	    /*
	    if (jo.has("actors")) {
    		
    		int numOfCast = jo.get("actors").getAsJsonArray().size();
    		
    		System.out.println("numOfCast:" + numOfCast);
    		
    		for(int i=0; i < numOfCast; i++) {
    			series.getCasts().add(jo.get("actors").getAsJsonArray().get(i).getAsString());
    			System.out.println(jo.get("actors").getAsJsonArray().get(i).getAsString());
    		}
	    }
	    
	    */
	    
	    System.out.println("imdb actors");
	    
	    if (jo.has("plot")) {
    		
    		series.setSummary(jo.get("plot").getAsString());
    		System.out.println(jo.get("plot").getAsString());
	    }
	    
	    System.out.println("imdb plot");
	    
	    series.save();
	    
	  }
	
	public static void fillSeries(Serie series) {


	    WSRequest request = WS.url("http://www.omdbapi.com/?t="+ series.getName() +"&plot=full");
	    request.mimeType = "application/json";
	    
	    HttpResponse response = null;
	    
	    try {
	    	response = request.get();
	    }
	    catch (Exception exc) {
	        System.err.println("error in imdb json");
	    }


	    if (response.getStatus() != 200) {
	    	return;
	    }
	    
	    JsonElement je = response.getJson();

	    
	    if ((je == null) || !je.isJsonObject())
	    	return;
	    
	    
	    if ((je.getAsJsonObject().has("Actors"))) {
    		
    		series.setCasts(je.getAsJsonObject().get("Actors").getAsString());
    	
	    }
	    
	    
	    /*
	    if ((je.getAsJsonObject().has("Actors"))) {
    		
    		int numOfCast = je.getAsJsonObject().get("Actors").getAsJsonArray().size();
    		
    		for(int i=0; i < numOfCast; i++)
	    	series.getCasts().add(je.getAsJsonObject().get("Actors").getAsJsonArray().get(i).getAsString());
	    }
	    */
	    
	    if ((je.getAsJsonObject().has("Plot"))) {
    		
    		series.setSummary(je.getAsJsonObject().get("Plot").getAsString());
    	
	    }
	    
	    
	    series.save();
	    
	  }
	
}
